#include "verticalstacklayout.h"

#include "../VisBaseClasses/visbaseelement.h"

VerticalStackLayout::VerticalStackLayout(QObject *parent) :
    VisBaseLayout(parent)
{
    init();
}

VerticalStackLayout::VerticalStackLayout(QList< QPointer< VisBaseElement > > e, QObject *parent) :
    VisBaseLayout(parent)
{
    elementsToLayout.append(e);

    init();
}


//VerticalStackLayout::VerticalStackLayout( const VerticalStackLayout& other) :
//    VisBaseLayout(other), gapBetweenElements( other.gapBetweenElements), startEltIdx( other.startEltIdx), startYPos( other.startYPos ), elementsCentered(other.elementsCentered)
//{
//}

void VerticalStackLayout::init()
{
    gapBetweenElements = 2.0;
    startEltIdx = 0;
    startYPos = 0.0;
    elementsCentered = false;
}

//---------------------------SETTERS---------------------------------
void VerticalStackLayout::setStartEltIdx( int newStartEltIdx )
{
    startEltIdx = newStartEltIdx;
}

void VerticalStackLayout::setStartYPos( float newStartYPos )
{
    startYPos = newStartYPos;
}

void VerticalStackLayout::setGapBetweenElements( float newGapBetweenElements )
{
    gapBetweenElements = newGapBetweenElements;
}

void VerticalStackLayout::setElementsCentered( bool newElementsCentered )
{
    elementsCentered = newElementsCentered;
}


//-------------------------PROCESS LAYOUT---------------------------------
void VerticalStackLayout::process()
{
    if( elementsToLayout.size() == 0 )
    {
        qDebug() << "too few elements to layout " << endl;
        return;
    }

    if( elementsCentered )
        layoutCenteredElements();
    else
        layoutTopElements();
}

void VerticalStackLayout::layoutCenteredElements()
{
    qDebug() << " laying out elements " << elementsToLayout.size() << endl;

    //SET POSITION OF ELEMENTS
    //1: Give start position -- x is unchanged, y is set to startY
    elementsToLayout[startEltIdx]->setPos( elementsToLayout[startEltIdx]->pos().x(), startYPos );

    //2: Move backwards from startElement to 0
    if( startEltIdx > 0 )
    {
        float prevEltHeight = elementsToLayout[startEltIdx]->boundingRect().height();
        float currEltHeight;
        float currY = startYPos;
        for(int i = startEltIdx-1; i >= 0; i-- )
        {
            currEltHeight = elementsToLayout[i]->boundingRect().height();
            currY = currY - prevEltHeight/2.0 - currEltHeight/2.0 - gapBetweenElements;

            elementsToLayout[i]->setPos( elementsToLayout[i]->pos().x(), currY );

            prevEltHeight = currEltHeight;
        }
    }

    //3: Move forwards from startElement and set the other positions
    if( startEltIdx < elementsToLayout.size()-1 )
    {
        float prevEltHeight = elementsToLayout[startEltIdx]->boundingRect().height();
        float currEltHeight;
        float currY = startYPos;
        for( int i = startEltIdx+1; i < elementsToLayout.size(); i++ )
        {
            currEltHeight = elementsToLayout[i]->boundingRect().height();
            currY = currY + prevEltHeight/2.0 + currEltHeight/2.0 + gapBetweenElements;

            elementsToLayout[i]->setPos( elementsToLayout[i]->pos().x(), currY );

            prevEltHeight = currEltHeight;
        }
    }
}

void VerticalStackLayout::layoutTopElements()
{

    //SET POSITION OF ELEMENTS
    //1: Give start position -- x is unchanged, y is set to startY
    elementsToLayout[startEltIdx]->setPos( elementsToLayout[startEltIdx]->pos().x(), startYPos );

    //2: Move backwards from startElement to 0
    if( startEltIdx > 0 )
    {
        float currEltHeight;
        float currY = startYPos;
        for(int i = startEltIdx-1; i >= 0; i-- )
        {
            currEltHeight = elementsToLayout[i]->boundingRect().height();
            currY = currY - currEltHeight - gapBetweenElements;

            elementsToLayout[i]->setPos( elementsToLayout[i]->pos().x(), currY );
        }
    }

    //3: Move forwards from startElement and set the other positions
    if( startEltIdx < elementsToLayout.size()-1 )
    {
        float prevEltHeight = elementsToLayout[startEltIdx]->boundingRect().height();
        float currY = startYPos;
        for( int i = startEltIdx+1; i < elementsToLayout.size(); i++ )
        {
            currY = currY + prevEltHeight + gapBetweenElements;

            elementsToLayout[i]->setPos( elementsToLayout[i]->pos().x(), currY );

            prevEltHeight = elementsToLayout[i]->boundingRect().height();
        }
    }
}
